package com.iflytek.universitymanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iflytek.universitymanagement.domain.Student;
import com.iflytek.universitymanagement.domain.StudentCompetition;
import com.iflytek.universitymanagement.service.StudentCompetitionService;
import com.iflytek.universitymanagement.service.StudentService;
import com.iflytek.universitymanagement.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/student-competition")
public class StudentCompetitionController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentCompetitionService studentCompetitionService;

    @GetMapping("/list")
    public Result list(@RequestParam("studentName") String studentName) {
        // 根据学生姓名查询学生信息
        Student student = studentService.getStudentByName(studentName);
        if (student == null) {
            return Result.error("未找到该学生");
        }

        Integer studentId = student.getStudentId();  // 获取学生ID

        // 调用 Service 层的方法获取学生竞赛数据并封装竞赛名称
        List<Map<String, Object>> studentCompetitions = studentCompetitionService.getStudentCompetitionsWithDetails(studentId);

        if (studentCompetitions.isEmpty()) {
            return Result.error("未找到该学生的竞赛数据");
        }

        return Result.success(studentCompetitions);  // 返回包含竞赛名称的结果
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public com.iflytek.universitymanagement.config.Result deleteById(@PathVariable Integer id) {
        studentCompetitionService.removeById(id);
        return com.iflytek.universitymanagement.config.Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public com.iflytek.universitymanagement.config.Result deleteBatch(@RequestBody List<Integer> ids) {
        studentCompetitionService.removeByIds(ids);
        return com.iflytek.universitymanagement.config.Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public com.iflytek.universitymanagement.config.Result updateById(@RequestBody StudentCompetition studentCompetition) {
        studentCompetitionService.updateById(studentCompetition);
        return com.iflytek.universitymanagement.config.Result.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "5") int pageSize,
                             @RequestParam(required = false) String name,
                             @RequestParam(required = false) String competitionName) {
        IPage<StudentCompetition> page = studentCompetitionService.selectPage(pageNum, pageSize, name, competitionName);
        return Result.success(page);
    }


    @GetMapping("/data")
    public Result getStudentCompetitionData(@RequestParam("studentName") String studentName) {
        Map<String, Object> data = studentCompetitionService.getStudentCompetitionData(studentName);
        if (data.isEmpty()) {
            return new Result(404, "没有找到该学生的数据", null);
        }
        return new Result(200, "成功", data);
    }

    @PostMapping("/add")
    public Map<String, String> add(@RequestBody StudentCompetition studentCompetition) {
        boolean result = studentCompetitionService.add(studentCompetition);
        Map<String, String> response = new HashMap<>();
        response.put("message", result ? "Insert success" : "Insert failed");
        return response;
//        return result ? "添加成功" : "添加失败";
    }
}
